package com.homedo.odin.solution.service.bizservice.mng;

import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.BrandEntranceListReq;
import com.homedo.odin.solution.contract.mng.BrandEntranceListResp;
import com.homedo.odin.solution.contract.mng.BrandFirmsEntranceResp;
import com.homedo.odin.solution.contract.mng.BrandOwnEditReq;
import com.homedo.odin.solution.contract.oms.BrandOwnerDetailResp;
import com.homedo.odin.solution.contract.oms.BrandOwnerReq;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.model.dto.BrandEntranceBean;
import com.homedo.odin.solution.model.dto.BrandOwnerBean;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.model.entity.BrandType;
import com.homedo.odin.solution.model.response.pc.brand.BrandOwnerDetailResponse;
import com.homedo.odin.solution.service.BrandOwnerServiceImpl;
import com.homedo.odin.solution.service.BrandTypeServiceImpl;
import com.homedo.odin.solution.service.CommonTypeDataServiceImpl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: donglibing
 * @date: 2018/3/27 17:36
 * @Description:
 */
@Service
public class MngBrandBizService {


    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;

    @Autowired
    private CommonTypeDataServiceImpl commonDataService;

    @Autowired
    BrandTypeServiceImpl brandTypeService;

    public void addorUpdateBrandOwner(BrandOwnerReq brandAddBean, int brandId) {
        if (brandAddBean == null) {
            throw new BizException(Meta.CODE_451.getCode(), Meta.CODE_451.getMsg());
        }
        if (StringUtils.isEmpty(brandAddBean.getBrandName()) ||
                StringUtils.isEmpty(brandAddBean.getBrandIcon())) {
            throw new BizException(Meta.CODE_451.getCode(), Meta.CODE_451.getMsg());
        }

        Map<String, Object> map = new HashMap<>();
        map.put("brand_name", brandAddBean.getBrandName());
        map.put("is_del", 0);
        List<BrandOwner> brandOwners = brandOwnerService.selectByMap(map);
        if (brandOwners != null && !brandOwners.isEmpty()) {
            throw new BizException(Meta.CODE_451.getCode(), "该品牌名称已存在");
        }

        BrandOwner addBrandOwner = new BrandOwner();
        if (brandId > 0) {
            //判断是否存在
            BrandOwner brandOwner = brandOwnerService.getBrandOwnerById(brandId);
            if (brandOwner == null) {
                throw new BizException(Meta.CODE_451.getCode(), "不存在该品牌");
            }
            brandTypeService.deleteByBrandId(brandId);
            addBrandOwner.setId(brandId);
        }
        addBrandOwner.setBrandName(brandAddBean.getBrandName());
        addBrandOwner.setBrandIcon(brandAddBean.getBrandIcon());
        addBrandOwner.setCreateUserName(brandAddBean.getCreateUserName());
        brandOwnerService.insertOrUpdate(addBrandOwner);

        //处理品牌分类
        String ids = brandAddBean.getTypeIds();
        if (StringUtils.isNotEmpty(ids)) {
            String[] idArray = ids.split(",");
            HashMap<String, String> map1 = commonDataService.getDataById(8);
            for (String id : idArray) {
                BrandType brandType = new BrandType();
                brandType.setBrandId(addBrandOwner.getId());
                brandType.setTypeId(Integer.valueOf(id));
                brandType.setTypeName(map1.get(id));
                brandTypeService.insert(brandType);
            }
        }
    }

    public BrandOwnerDetailResp queryBrandOwnerDetail(Integer id) {
        BrandOwnerDetailResp resp = new BrandOwnerDetailResp();
        if (!NumberHelper.validatePositive(id)) {
            throw new BizException(Meta.CODE_451.getCode(), "参数不正确");
        }
        BrandOwner brandOwner = brandOwnerService.getBrandOwnerById(id);
        if (brandOwner == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该品牌商不存在");
        }

        resp.setId(brandOwner.getId());
        resp.setBrandName(brandOwner.getBrandName());
        resp.setBrandIcon(brandOwner.getBrandIcon());

        List<Integer> list = brandTypeService.getTypeIds8BrandId(brandOwner.getId());
        resp.setTypeIds(list);
        return resp;
    }

    public List<BrandOwnerDetailResponse> getBrandCal(Integer number) {
        List<BrandOwnerDetailResponse> responseList = new ArrayList<BrandOwnerDetailResponse>();
        List<BrandOwner> brandOwnerList = brandOwnerService.getBrandCal(number);
        brandOwnerList.forEach(brandOwner -> {
            BrandOwnerDetailResponse brandOwnerDetail = new BrandOwnerDetailResponse();
            BeanUtils.copyProperties(brandOwner, brandOwnerDetail);
            brandOwnerDetail.setBrandId(brandOwner.getId());
            brandOwnerDetail.setSolutionCount(brandOwner.getCount());
            brandOwnerDetail.setUserId(brandOwner.getUserId());
            responseList.add(brandOwnerDetail);
        });
        return responseList;
    }

    /**
     * @Description: OMS_获取品牌列表(显示的品牌都会拥有至少一个品牌厂商)
     * @Author: liyi
     */
    public BrandFirmsEntranceResp queryBrandFirmsEntrance() {
        BrandFirmsEntranceResp resp = new BrandFirmsEntranceResp();
        List<BrandOwnerBean> list = brandOwnerService.queryBrandFirmsEntrance();
        resp.setList(list);
        //获取最新的修改时间和修改人
        BrandOwner brandOwner = brandOwnerService.queryLateBrand();
        if (brandOwner != null) {
            resp.setModifiedUserName(brandOwner.getOperator());
            resp.setModifiedTime(brandOwner.getLastModifiedTime());
        }
        return resp;
    }

    /**
     * @Description: OMS_修改品牌序号
     * @Author: liyi
     */
    public void editBrandSort(BrandOwnEditReq brandOwnEditReq) {
        Integer brandId = brandOwnEditReq.getBrandId();
        BrandOwner brandOwner = brandOwnerService.findById(brandId);
        if (brandOwner == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "不存在该品牌");
        }
        brandOwner.setSort(brandOwnEditReq.getSort());
        brandOwner.setLastModifiedTime(new Date());
        brandOwner.setModifiedBy(brandOwnEditReq.getModifiedBy());
        brandOwner.setOperator(brandOwnEditReq.getOperator());
        brandOwnerService.updateById(brandOwner);
    }

    /**
     * @Description: OMS_获取品牌列表(大牌原厂入口管理)分页
     * @Author: liyi
     */
    public BrandEntranceListResp queryBrandEntranceList(BrandEntranceListReq req) {
        PageBean pageBean = new PageBean(req.getPageNum(), req.getPageSize());
        PageInfo<BrandEntranceBean> queryResult = brandOwnerService
                .queryBrandEntranceList(req.getSortName(), req.getSort(), pageBean);
        return BeanUtils.transfrom(BrandEntranceListResp.class, queryResult);
    }
}
